package com.shengyin.ec.service.permission;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shengyin.ec.model.dao.permission.ResourcesDao;
import com.shengyin.ec.model.entity.Resources;

@Service(value="resourcesService")
public class ResourcesServiceImpl implements ResourcesService {

	@Autowired
	ResourcesDao resourcesDao;
	
	
	@Override
	public void createResources(Resources resources) {
		resourcesDao.save(resources);		
	}

	@Override
	public void deleteResources(Resources resources) {
		Resources users=resourcesDao.get(resources.getId());
		resourcesDao.delete(users);
		
	}

	@Override
	public void deleteResourcesById(long id) {
		resourcesDao.deleteByKey(id);		
	}

	@Override
	public void modifyResources(Resources resources) {
		
		Resources users=resourcesDao.get(resources.getId());
		try {
			BeanUtils.copyProperties(users, resources);
			resourcesDao.update(users);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}		
	}

	@Override
	public Resources getResourcesById(long id) {
		return resourcesDao.get(id);
	}

	@Override
	public List<Resources> getResourcesList() {
		return resourcesDao.loadAll();
	}

	@Override
	public List<Resources> getResourcesList(int start, int end) {
		return resourcesDao.getResourcesList(start,end);
	}

	@Override
	public List<Resources> getResourcesListByKeyword(String keyword, int start, int end) {
		return resourcesDao.getResourcesListByKeyword(keyword, start, end);
	}

	@Override
	public void deleteSelectedResources(Long[] ids) {
		resourcesDao.deleteSelectedResources(ids);		
	}

	@Override
	public Long getTotal() {
		return  resourcesDao.getTotal();		
	}


	@Override
	public List<Resources> getResourcesListByRole(Long roleId, int start,
			int end) {
		Set<Resources> resources =resourcesDao.getResourcesListByRole(roleId, start, end);
		List<Resources> list = new ArrayList<Resources>(resources); 
		return list;
	}

}
